<?php
namespace app\common\model;
use think\Model;
use think\Session;
/**
 * 实名认证
 */
class Authentication extends Model
{
	// 接口说明
	// 接口地址：http://api.id98.cn/api/idcard
	// 请求方式：get/post
	// 返回格式：json/xml
	// 编码类型：utf-8 
	// 接口返回参数
	// {
	// "isok":1,
	// "code":1,
	// "data":
	// {"err":0,
	// "address":"\u5c71\u4e1c\u7701\u6cf0\u5b89\u5e02\u5cb1\u5cb3\u533a",
	// "sex":"M",
	// "birthday":"1986-02-12"}
	// }
	private $url = 'http://api.id98.cn/api/idcard';
    private $parameter = '';
    private $appkey = '94b026f08aaa8897ec9c096205c6959a';
	/**
	 * [CheckPhoneCode 校验身份证]
	 * @param [type] $loc_code [description]
	 * @param [type] $pho_code [description]
	 */
	public function Check($user_id,$name,$ID_number,$method='post')
	{
		//检测用户是否实名认证通过
		$authentication = db('users')->where('id',$user_id)->value('authentication');
		if ($authentication) {
			return $this->success('该用户已经实名认证');
		}
		$res = ['isok'=>1,'code'=>'','messsage'=>''];
		$this->parameter = array(
                'appkey' => $this->appkey,
                'name' => $name,
                'cardno' => $ID_number,
        );
		if ($method == 'post') {
            $r = \services\Http::post($this->url, $this->parameter);
        } else {
            $r = \services\Http::get($this->url, $this->parameter);
        }
        $result = json_decode($r);
        $code = $result->code;
        $isok = $result->isok;

        $res = ['isok'=>$isok,'code'=>$code ,'messsage'=>$this->getCodeMean($code)];
        return $res;

        //其他
        if ($isok==1&&$code==1) {
        	//身份一致
        	$res = ['isok'=>$isok,'code'=>$code ,'messsage'=>$this->getCodeMean($code)];
        	return $res;
        }else{
        	//身份不一致
        	$res = ['isok'=>$isok,'code'=>$code ,'messsage'=>$this->getCodeMean($code)];
        	return $res;
        }
	}
	/**
	 * [getCodeMean code含义]
	 * @param string $value [description]
	 */
	public function getCodeMean($value)
	{
		$code= ['1'=>'一致','2'=>'不一致','3'=>'无此身份证号码','11'=>'参数不正确','12'=>'商户余额不足','13'=>'appkey不存在','14'=>'IP被拒绝','20'=>'身份证中心维护中'];
        return $code[$value];
	}
 /**
   * 判断是否为合法的身份证号码(正则校验)
   * @param $mobile
   * @return int
   */
  public function isCreditNo($vStr){
    $vCity = array(
      '11','12','13','14','15','21','22',
      '23','31','32','33','34','35','36',
      '37','41','42','43','44','45','46',
      '50','51','52','53','54','61','62',
      '63','64','65','71','81','82','91'
    );
    if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
    if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
    $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
    $vLength = strlen($vStr);
    if ($vLength == 18) {
      $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
    } else {
      $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
    }
    if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
    if ($vLength == 18) {
      $vSum = 0;
      for ($i = 17 ; $i >= 0 ; $i--) {
        $vSubStr = substr($vStr, 17 - $i, 1);
        $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
      }
      if($vSum % 11 != 1) return false;
    }
    return true;
  }
  
}